public class EditPayoutVF
{
    private final Plan__c[] plan = new Plan__c[1];
    
    public Plan__c planObj {get; set;}
    public Rate__c rateObj {get; set;}
    public String rateType {get; set;}
    
    public Boolean displayAttainmentTable {get; set;}
    public Boolean displayCreditTable {get; set;}

    public List<AttainmentWrapper> payoutAttainmentList {get; set;}
    public List<CreditWrapper> payoutCreditList {get; set;}
    private List<AttainmentWrapper> attainmentList = new List<AttainmentWrapper>();
    private List<CreditWrapper> creditList = new List<CreditWrapper>();
    
    public EditPayoutVF(ApexPages.StandardController myController)
    {
        plan[0] = (Plan__c)myController.getRecord();
        planObj = [select id, Quota__c, OTE__c from Plan__c where id = :plan[0].id];

        displayAttainmentTable = true;
        displayCreditTable = false;
        
        if(payoutAttainmentList == null)
            payoutAttainmentList = new List<AttainmentWrapper>();
        if(payoutCreditList == null)
            payoutCreditList = new List<CreditWrapper>();
        
        Rate__c[] rateList = [select Min_Attainment__c, Max_Attainment__c, Min_Credit__c, Max_Credit__c, Rate_Type__c, Rate__c from Rate__c where Plan__c = :plan[0].id];
        if(rateList.size() > 0)
        {
            rateType = rateList[0].Rate_Type__c;
            if(rateList[0].Rate_Type__c == 'Credit')
            {
                for(Rate__c r: rateList)
                {
                    CreditWrapper c = new CreditWrapper();
                    c.min = r.Min_Credit__c;
                    c.max= r.Max_Credit__c;
                    c.Rate = r.Rate__c;
                    payoutCreditList.add(c);
                }
                displayAttainmentTable = !displayAttainmentTable;
                displayCreditTable = !displayCreditTable;
                
            }
            else
                for(Rate__c r: rateList)
                {
                    AttainmentWrapper a = new AttainmentWrapper();
                    a.min = r.Min_Credit__c;
                    a.max= r.Max_Credit__c;
                    a.Rate = r.Rate__c;
                    payoutAttainmentList.add(a);
                }
        }
        else
        {
            payoutAttainmentList.add(new AttainmentWrapper());
        }
        
    }
    
    public List<SelectOption> getRateTypeList()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Attainment', 'Attainment'));
        options.add(new SelectOption('Credit', 'Credit'));
        return options;
    }
    
    public PageReference addPayoutType()
    {
        if(rateType == 'Attainment')
            payoutAttainmentList.add(new AttainmentWrapper());
        else
            payoutCreditList.add(new CreditWrapper());
        return null;
    }
    
    public class AttainmentWrapper
    {
        public Double min {get; set;}
        public Double max {get; set;}
        public Double Rate {get; set;}
        public AttainmentWrapper()
        {
            min = 0.0;
            max = 0.0;
            Rate = 0.0;
        }
    }
    
    public class CreditWrapper
    {
        public Double min {get; set;}
        public Double max {get; set;}
        public Double Rate {get; set;}
        public CreditWrapper()
        {
            min = 0.0;
            max = 0.0;
            Rate = 0.0;
        }

    }

    public List<AttainmentWrapper> getAttainmentRows() {
        return payoutAttainmentList;
    }
    
    public List<CreditWrapper> getCreditRows() {
        return payoutCreditList;
    }

    public PageReference checkSelection()
    {
        displayAttainmentTable = !displayAttainmentTable;
        displayCreditTable = !displayCreditTable;
        if(rateType == 'Attainment')
        {
            for(CreditWrapper aw: payoutCreditList)
                creditList.add(aw);
            payoutCreditList.clear();
            if(attainmentList.size() > 0)
            {
                for(AttainmentWrapper aw: attainmentList)
                    payoutAttainmentList.add(aw);
                attainmentList.clear();
            }
        }
        else
        {
            for(AttainmentWrapper aw: payoutAttainmentList)
                attainmentList.add(aw);
            payoutAttainmentList.clear();                    

            if(creditList.size() == 0 && payoutCreditList.size() == 0)
            {
                payoutCreditList.add(new CreditWrapper());
            }
            
            if(creditList.size() > 0)
            {
                for(CreditWrapper aw: creditList)
                    payoutCreditList.add(aw);
                creditList.clear();
            }
            
        }
        return null;
    }    

    public PageReference save()
    {
        for(Rate__c[] rList : [select id from Rate__c where Plan__c = :plan[0].id])
            delete rList;
            
        Rate__c[] rateList = new Rate__c[]{};
        update PlanObj;
                
        if(rateType == 'Attainment')
        {
            for(AttainmentWrapper aw: payoutAttainmentList)
            {
                if(aw.min > 0 && aw.max > 0 && aw.Rate > 0)
                {
                    Rate__c rate = new Rate__c(Plan__c = plan[0].Id, Min_Attainment__c = aw.min, Max_Attainment__c = aw.max, Rate__c = aw.Rate, Rate_Type__c = rateType);
                    rateList.add(rate);                    
                }               
            }
        }
        else
        {
            for(CreditWrapper cw: payoutCreditList)
            {
                if(cw.min > 0 && cw.max > 0 && cw.Rate > 0)
                {
                    Rate__c rate = new Rate__c(Plan__c = plan[0].Id, Min_Credit__c = cw.min, Max_Credit__c = cw.max, Rate__c = cw.Rate, Rate_Type__c = rateType);
                    rateList.add(rate);                    
                }               
            }
        }
        if(rateList.size() > 0)
            insert rateList;
        
        
        return null;
    }
    
}